#include <iostream>
#include "pro.hpp"
#include <string>
#include <strings.h>
#include <cerrno>
#include <cstring>
#include <cstdlib>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <memory>

// 服务器端功能
// void server_side(int server_socket, struct sockaddr_in client_addr) {
//     TimeInfo time_info;
//     socklen_t client_addr_len = sizeof(struct sockaddr_in);

//     // 接收客户端发送的时间
//     recvfrom(server_socket, &time_info, sizeof(TimeInfo), 0, (struct sockaddr *)&client_addr, &client_addr_len);

//     // 获取服务器当前时间
//     time_info.server_time = time(NULL);

//     // 将服务器时间发送回客户端
//     sendto(server_socket, &time_info, sizeof(TimeInfo), 0, (struct sockaddr *)&client_addr, client_addr_len);
// }

namespace Server
{
    using namespace std;

    static const string defaultIp = "0.0.0.0";
    static const int gnum = 1024;

    class udpServer
    {
    public:
        udpServer(const uint16_t &port, const string &ip = defaultIp)
            : _port(port), _ip(ip), _sockfd(-1)
        {
        }
        void initServer()
        {
            // 1. 创建socket
            _sockfd = socket(AF_INET, SOCK_DGRAM, 0);
            if (_sockfd == -1)
            {
                cerr << "socket error: " << errno << " : " << strerror(errno) << endl;
                exit(-1);
            }
            cout << "socket success: "
                 << " : " << _sockfd << endl;

            // 2. 绑定port，ip
            struct sockaddr_in local;
            bzero(&local, sizeof(local));//memset设置全0
            local.sin_family = AF_INET;
            local.sin_port = htons(_port);
            // local.sin_addr.s_addr = inet_addr(_ip.c_str());
            local.sin_addr.s_addr = htonl(INADDR_ANY);
            int n = bind(_sockfd, (struct sockaddr *)&local, sizeof(local));
            if (n == -1)
            {
                cerr << "bind error: " << errno << " : " << strerror(errno) << endl;
                exit(-1);
            }
        }
        void start()
        {
            while(1)
            {
                struct sockaddr_in peer;
                socklen_t len = sizeof(peer);
                char inbuffer[BUFFSIZE];
                while(1)
                {
                    // server_side(_sockfd, peer);
                    // sleep(2);
                    std::memset(inbuffer, 0, sizeof(inbuffer));
                    ssize_t recv_len = recvfrom(_sockfd, inbuffer, sizeof(inbuffer) - 1, 0, (struct sockaddr*)&peer, &len);
                    if (recv_len < 0) {
                        std::cerr << "Error: Cannot receive data."<< std::endl;
                        continue;
                    }

                    std::string request_header(inbuffer);
                    Request request;
                    request.unwrap(request_header);

                    std::cout << "Server received: "<< request.message<< std::endl;

                    Response response("Hello from server!I can get u!");
                    std::string response_header = response.wrap();

                    // sleep(3);
                    sendto(_sockfd, response_header.c_str(), response_header.size(), 0, (struct sockaddr*)&peer, len);
                }
                // close(server_fd);
            }
        }
        ~udpServer() {}

    private:
        uint16_t _port;
        string _ip;
        int _sockfd;
    };
}
